package com.own.core.html.table;

import org.apache.wicket.AttributeModifier;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.navigation.paging.AjaxPagingNavigator;
import org.apache.wicket.extensions.markup.html.repeater.data.table.AbstractToolbar;
import org.apache.wicket.extensions.markup.html.repeater.data.table.DataTable;
import org.apache.wicket.extensions.markup.html.repeater.data.table.NavigationToolbar;
import org.apache.wicket.extensions.markup.html.repeater.data.table.NavigatorLabel;
import org.apache.wicket.markup.html.WebComponent;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.navigation.paging.PagingNavigator;
import org.apache.wicket.model.Model;


/**
 * Toolbar that displays (Ajax) links used to navigate the pages of the datatable as well as a
 * message about which rows are being displayed and their total number in the data table.
 * 
 * @author Igor Vaynberg (ivaynberg)
 * @author Martijn Dashorst (dashorst)
 * @since 1.2.1
 */
public class OwnAjaxNavigationToolbar extends AbstractToolbar
{
	private static final long serialVersionUID = 1L;
 

	private final DataTable<?> table;

	/**
	 * Constructor.
	 * 
	 * @param table
	 *            data table this toolbar will be attached to
	 */
	public OwnAjaxNavigationToolbar(final DataTable<?> table)
	{
		super(table);
		this.table = table;

		WebMarkupContainer span = new WebMarkupContainer("span");
		add(span);
		span.add(new AttributeModifier("colspan", true, new Model<String>(
			String.valueOf(table.getColumns().size()))));

		span.add(newOwnAjaxPagingNavigator("navigator", table));
		span.add(newNavigatorLabel("navigatorLabel", table));
	}

	
	



	/**
	 * Factory method used to create the navigator label that will be used by the datatable
	 * 
	 * @param navigatorId
	 *            component id navigator label should be created with
	 * @param table
	 *            dataview used by datatable
	 * @return navigator label that will be used to navigate the data table
	 * 
	 */
	protected WebComponent newNavigatorLabel(String navigatorId, final DataTable<?> table)
	{
		return new NavigatorLabel(navigatorId, table);
	}

	/**
	 * @see org.apache.wicket.Component#callOnBeforeRenderIfNotVisible()
	 */
	protected boolean callOnBeforeRenderIfNotVisible()
	{
		return true;
	}

	/** {@inheritDoc} */
	@Override
	protected void onBeforeRender()
	{
		setVisible(table.getPageCount() > 1);
		super.onBeforeRender();
	}

	/**
	 * Factory method used to create the paging navigator that will be used by the datatable.
	 * 
	 * @param navigatorId
	 *            component id the navigator should be created with
	 * @param table
	 *            dataview used by datatable
	 * @return paging navigator that will be used to navigate the data table
	 */
	 
	protected OwnAjaxPagingNavigator newOwnAjaxPagingNavigator(String navigatorId, final DataTable<?> table)
	{
		return new OwnAjaxPagingNavigator(navigatorId, table)
		{
			private static final long serialVersionUID = 1L;

			/**
			 * Implement our own ajax event handling in order to update the datatable itself, as the
			 * default implementation doesn't support DataViews.
			 * 
			 * @see AjaxPagingNavigator#onAjaxEvent(AjaxRequestTarget)
			 */
			@Override
			protected void onAjaxEvent(AjaxRequestTarget target)
			{
				target.addComponent(table);
			}
		};
	}
}
